import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import Components from 'unplugin-vue-components/vite'
export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd(), '')
  return {
    plugins: [
      vue(),
      Components({
        //自动加载的组件目录，默认值为 ['src/components']
        dirs: ['src/components'],
        //组件名称包含目录，防止同名组件冲突
        directoryAsNamespace: true,

        //指定类型声明文件，为true时在项目根目录创建
      }),
    ],

    resolve: {
      alias: {
        //设置别名
        '@': path.resolve(__dirname, './src'),
        '@assets': path.resolve(__dirname, './src/assets'),
      },
    },

    build: {
      assetsDir: 'static',
      rollupOptions: {
        output: {
          // 对静态文件进行打包处理（文件分类）
          // 此处打开后会导致背景图路径有问题，所以暂时隐藏，未找到合适的解决方案
          chunkFileNames: 'static/js/app-[hash].js',
          entryFileNames: 'static/js/app-[hash].js',
          assetFileNames: 'static/[ext]/app-[hash].[ext]',
        },
      },
    },

    server: {
      host: '0.0.0.0',
      port: '8080',
      proxy: {
        api: {
          target: 'http://localhost:3000',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    },
  }
})
